<script src="../../../resources/testharness.js"></script>
<script src="../../../resources/testharnessreport.js"></script>
<style>
    * {
        margin: 0;
    }
    #pusher {
        width: 1000px;
        height: 1000px;
        outline: 1px solid black;
    }
</style>
<div id="console"></div>
<div id="testArea">
    <div id="pusher">This box is here to create scrollbars.</div>
</div>
<script>
    const floatPrecision = 0.00001;

    function sendMouseDownAt(x, y) {
        return new Promise(function(resolve, reject) {
            if (window.chrome && chrome.gpuBenchmarking) {
              var pointerActions =
                  [{source: "pen",
                    actions: [
                      { name: "pointerDown", x: x, y: y },
                      { name: "pointerUp" },
                  ]}];
              chrome.gpuBenchmarking.pointerActionSequence(pointerActions, resolve);
            }
            else {
                reject();
            }
        });
    }

    function PointerDown(event) {
        eventFired = true;
        lastEvent = event;
    }

    window.addEventListener("pointerdown", PointerDown, false);

    promise_test(async () => {
        eventFired = false;
        await sendMouseDownAt(100, 100);
        assert_true(eventFired);
        assert_equals(lastEvent.clientX, 100, "clientX");
        assert_equals(lastEvent.clientY, 100, "clientY");
        assert_equals(lastEvent.pageX, 100, "pageX");
        assert_equals(lastEvent.pageY, 100, "pageY");
        assert_equals(lastEvent.offsetX, 100, "offsetX");
        assert_equals(lastEvent.offsetY, 100, "offsetY");
    }, 'Default');

    promise_test(async () => {
        eventFired = false;
        testRunner.setPageZoomFactor(1.2);
        await sendMouseDownAt(100, 100);
        assert_true(eventFired);
        assert_approx_equals(lastEvent.clientX, 83.33333, floatPrecision, "clientX");
        assert_approx_equals(lastEvent.clientY, 83.33333, floatPrecision, "clientY");
        assert_approx_equals(lastEvent.pageX, 83.33333, floatPrecision, "pageX");
        assert_approx_equals(lastEvent.pageY, 83.33333, floatPrecision, "pageY");
        assert_approx_equals(lastEvent.offsetX, 83.33333, floatPrecision, "offsetX");
        assert_approx_equals(lastEvent.offsetY, 83.33333, floatPrecision, "offsetY");
    }, 'Zoomed');

    promise_test(async () => {
        eventFired = false;
        window.scrollTo(50, 50);
        testRunner.setPageZoomFactor(1.0)
        await sendMouseDownAt(100, 100);
        assert_true(eventFired);
            assert_equals(lastEvent.clientX, 100, "clientX");
            assert_equals(lastEvent.clientY, 100, "clientY");
            assert_equals(lastEvent.pageX, 150, "pageX");
            assert_equals(lastEvent.pageY, 150, "pageY");
            assert_equals(lastEvent.offsetX, 150, "offsetX");
            assert_equals(lastEvent.offsetY, 150, "offsetY");
        window.scrollTo(0, 0);
    }, 'Scrolled');

    promise_test(async () => {
        eventFired = false;
        testRunner.setPageZoomFactor(1.2);
        window.scrollTo(50, 50);
        await sendMouseDownAt(100, 100);
        assert_true(eventFired);
        assert_approx_equals(lastEvent.clientX, 83.33333, floatPrecision, "clientX");
        assert_approx_equals(lastEvent.clientY, 83.33333, floatPrecision, "clientY");
        assert_approx_equals(lastEvent.pageX, 133.33333, floatPrecision, "pageX");
        assert_approx_equals(lastEvent.pageY, 133.33333, floatPrecision, "pageY");
        assert_approx_equals(lastEvent.offsetX, 133.33333, floatPrecision, "offsetX");
        assert_approx_equals(lastEvent.offsetY, 133.33333, floatPrecision, "offsetY");
        testRunner.setPageZoomFactor(1.0)
        window.scrollTo(0, 0);
    }, 'Zoomed and scrolled');
</script>
